<template>
  <div class="app-container">
    <el-card header="查询条件" shadow="never">
      <el-row :gutter="20">
        <el-col :span="3">
          <el-select v-model="listQuery.staton" placeholder="能源站类型" @focus="focus">
            <el-option label="全部" value="1" />
            <el-option label="1号能源站" value="2" />
            <el-option label="2号能源站" value="3" />
            <el-option label="3号能源站" value="4" />
          </el-select>
        </el-col>
        <el-col :span="12">
          <el-button type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="handleCreate">添加</el-button>
          <el-button type="primary" icon="el-icon-s-promotion">导出</el-button>
        </el-col>
      </el-row>
    </el-card>
    <!-- 表格 -->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="tableList"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column prop="id" label="ID" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="station" label="能源站" width="120">
        <template slot-scope="{row}">
          <span>{{ row.station }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="systemName" label="系统名称" width="100">
        <template slot-scope="{row}">
          <span>{{ row.systemName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="moduleName" label="模块名称" width="100">
        <template slot-scope="{row}">
          <span>{{ row.moduleName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="deviceId" label="设备编号" width="100">
        <template slot-scope="{row}">
          <span>{{ row.deviceId }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="heat" label="供热量" width="100">
        <template slot-scope="{row}">
          <span>{{ row.heat }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="heatElectricpower" label="供热电功率" width="100">
        <template slot-scope="{row}">
          <span>{{ row.heatElectricpower }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="heatGaspower" label="供热耗气量" width="100">
        <template slot-scope="{row}">
          <span>{{ row.heatGaspower }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="municipalHeat" label="市政供热量" width="100">
        <template slot-scope="{row}">
          <span>{{ row.municipalHeat }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="heatCop" label="供热COP" width="100">
        <template slot-scope="{row}">
          <span>{{ row.heatCop }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="cold" label="供冷量" width="100">
        <template slot-scope="{row}">
          <span>{{ row.cold }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="coldElectricpower" label="供冷耗电量" width="100">
        <template slot-scope="{row}">
          <span>{{ row.coldElectricpower }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="coldGaspower" label="供冷耗气量" width="100">
        <template slot-scope="{row}">
          <span>{{ row.coldGaspower }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="coldCop" label="供冷COP" width="100">
        <template slot-scope="{row}">
          <span>{{ row.coldCop }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="energyType" label="能源类型">
        <template slot-scope="{row}">
          <span>{{ row.energyType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="160px">
        <template slot-scope="{row,$index}">
          <el-button mc-type="column-el-button" size="mini" type="primary" @click="handleUpdate(row)">编辑</el-button>
          <el-button mc-type="column-el-button" size="mini" type="danger" @click="handleDelete(row,$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 弹框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="能源站类型" prop="station">
          <el-select v-model="temp.station" placeholder="请选择" @focus="focus">
            <el-option label="1号能源站" value="2" />
            <el-option label="2号能源站" value="3" />
            <el-option label="3号能源站" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="系统名称" prop="systemName">
          <el-input v-model="temp.systemName" />
        </el-form-item>
        <el-form-item label="耗能量" prop="consumption">
          <el-input v-model="temp.consumption" />
        </el-form-item>
        <el-form-item label="产能效率" prop="supplyRate">
          <el-input v-model="temp.supplyRate" />
        </el-form-item>
        <el-form-item label="最长开启时间" prop="maxTime">
          <el-input v-model="temp.maxTime" />
        </el-form-item>
        <el-form-item label="最小开启时间" prop="minTime">
          <el-input v-model="temp.minTime" />
        </el-form-item>
        <el-form-item label="能源类型" prop="energyType">
          <el-input v-model="temp.energyType" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          Cancel
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          Confirm
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { energySystemList } from '@/api/cfg'

export default {
  name: 'UserConfig',
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        username: undefined,
        userType: undefined,
        sort: '+id'
      },
      temp: {
        id: undefined,
        importance: 1,
        station: '',
        systemName: ''
      },
      dialogFormVisible: false, // 弹框是否显示
      dialogStatus: '', // 弹框类型：修改/新增
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      tableList: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取数据
    getList() {
      this.listLoading = true
      energySystemList(this.listQuery).then(response => {
        this.tableList = response.data.items
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        status: 'published',
        type: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.dialogFormVisible = false
      this.$notify({
        title: 'Success',
        message: 'Created Successfully',
        type: 'success',
        duration: 2000
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.dialogFormVisible = false
      this.$notify({
        title: 'Success',
        message: 'Update Successfully',
        type: 'success',
        duration: 2000
      })
    },
    handleDelete(row, index) {
      this.$notify({
        title: 'Success',
        message: 'Delete Successfully',
        type: 'success',
        duration: 2000
      })
      this.list.splice(index, 1)
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
